﻿using System;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Text;
using System.Web;
using System.Web.Services;

using com.vs.DataAccess;
using com.vs.DataAccess.DO;
using com.vs.General;
using com.vs.Receiving.BL;

namespace com.vs.WebService
{
    /// <summary>
    /// Summary description for ReceiptHeaderService
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(false)]
    // To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 
    // [System.Web.Script.Services.ScriptService]
    public class ReceivingService : System.Web.Services.WebService
    {
        string XSLT_DIR = "xslt";


        [WebMethod]
        public string GetPOHdrs(string stSessID, string stCondXML)
        {
            List<KeyValuePair<string, KeyValuePair<string, object>>> dcCond = null;
            DataTable dt = null;
            Session sess = null;
            StringBuilder sbXML = null;
            POManager poMgr = null;
            string stErr = null;

            try
            {
                sess = (Session)Application.Get(stSessID);
                if (null == sess)
                    return MessageManager.MSG_SES_001;

                dcCond = new List<KeyValuePair<string, KeyValuePair<string, object>>>();
                if (!StringManager.IsEmpty(stCondXML))
                    dcCond = XMLDocManager.BuildQueryCondition(stCondXML);

                poMgr = new POManager();
                dt = poMgr.GetPOHdrsByQFCri(sess, dcCond, null, ref stErr);
                if (!StringManager.IsEmpty(stErr))
                    return stErr;
                //dt = PurchaseOrderHeaderDO.GetTableByColumns(sess, dcCond);
                if (DataManager.IsEmpty(dt))
                    return null;
                sbXML = new StringBuilder();
                dt.TableName = "PURCHASE_ORDER_HEADER";
                dt.DataSet.DataSetName = "PURCHASE_ORDER_HEADERS";
                dt.DataSet.WriteXml(new StringWriter(sbXML));
                return sbXML.ToString();
            }
            catch (Exception e)
            {
                return MessageManager.MSG_GEN_000 + e.Message;
            }
            finally
            {
            }
        }

        [WebMethod]
        public string SavePOHdr(string stSessID, string stXML)
        {
            PurchaseOrderHeaderDO poHdrDO = null;
            Session sess = null;
            StringBuilder sbXML = new StringBuilder();

            try
            {
                sess = (Session)Application.Get(stSessID);
                if (null == sess)
                    return MessageManager.MSG_SES_001;

                poHdrDO = (PurchaseOrderHeaderDO)XMLDocManager.Deserialize(typeof(PurchaseOrderHeaderDO), XMLDocManager._XML_HEADER + stXML);
                if (poHdrDO.INTERNAL_PURCHASE_ORDER_NUM > 0)
                {
                    poHdrDO.Update(sess);
                    return "" + poHdrDO.INTERNAL_PURCHASE_ORDER_NUM;
                }
                else
                    return "" + poHdrDO.Insert(sess);
            }
            catch (Exception e)
            {
                return MessageManager.MSG_GEN_000 + e.Message;
            }
        }

        [WebMethod]
        public string SavePODtl(string stSessID, string stRcpDtlXML)
        {
            PurchaseOrderDetailDO poDtlDO = null;
            Session sess = null;
            StringBuilder sbXML = new StringBuilder();
            POManager pdMgr = null;

            try
            {
                poDtlDO = (PurchaseOrderDetailDO)XMLDocManager.Deserialize(typeof(PurchaseOrderDetailDO), XMLDocManager._XML_HEADER + stRcpDtlXML);

                sess = (Session)Application.Get(stSessID);
                if (null == sess)
                    return MessageManager.MSG_SES_001;

                pdMgr = new POManager();
                return pdMgr.SavePODetail(sess, poDtlDO);
            }
            catch (Exception e)
            {
                return MessageManager.MSG_GEN_000 + e.Message;
            }
        }

        [WebMethod]
        public string GetPODtls(string stSessID, string stIntPONum)
        {
            Session sess = null;
            DataTable dt = null;
            StringBuilder sbXML = null;
            int iIntRecNum = 0;

            try
            {
                if (StringManager.IsEmpty(stIntPONum))
                    return MessageManager.MSG_RCP_009;

                iIntRecNum = int.Parse(stIntPONum);
                if (iIntRecNum < 1)
                    return MessageManager.MSG_RCP_009;

                sess = (Session)Application.Get(stSessID);
                if (null == sess)
                    return MessageManager.MSG_SES_001;

                dt = PurchaseOrderDetailDO.GetTableByColumn(sess, "INTERNAL_PURCHASE_ORDER_NUM", stIntPONum);
                if (DataManager.IsEmpty(dt))
                    return null;
                sbXML = new StringBuilder();
                dt.TableName = "PURCHASE_ORDER_DETAIL";
                dt.DataSet.DataSetName = "PURCHASE_ORDER_DETAILS";
                dt.DataSet.WriteXml(new StringWriter(sbXML));

                return sbXML.ToString();
            }
            catch (Exception e)
            {
                return MessageManager.MSG_GEN_000 + e.Message;
            }
            finally
            {
            }
        }

        [WebMethod]
        public string GetReceiptHdrs(string stSessID, string stCondXML)
        {
            List<KeyValuePair<string, KeyValuePair<string, object>>> dcCond = null;
            DataTable dt = null;
            Session sess = null;
            StringBuilder sbXML = null;
            ReceiptManager rptMgr = null;
            string stError = null;

            try
            {
                sess = (Session)Application.Get(stSessID);
                if (null == sess)
                    return MessageManager.MSG_SES_001;

                dcCond = new List<KeyValuePair<string, KeyValuePair<string, object>>>();
                if (!StringManager.IsEmpty(stCondXML))
                    dcCond = XMLDocManager.BuildQueryCondition(stCondXML);

                rptMgr = new ReceiptManager();
                dt = rptMgr.GetReceiptHdrsByQFCri(sess, dcCond, null, ref stError);
                if (DataManager.IsEmpty(dt))
                {
                    if (StringManager.IsEmpty(stError))
                        return null;
                    else
                        return stError;
                }
                sbXML = new StringBuilder();
                dt.TableName = "RECEIPT_HEADER";
                dt.DataSet.DataSetName = "RECEIPT_HEADERS";
                dt.DataSet.WriteXml(new StringWriter(sbXML));
                return sbXML.ToString();
            }
            catch (Exception e)
            {
                return MessageManager.MSG_GEN_000 + e.Message;
            }
            finally
            {
            }
        }

        [WebMethod]
        public string SaveReceiptHdr(string stSessID, string stXML)
        {
            ReceiptHeaderDO rcpHdrDO = null;
            Session sess = null;
            StringBuilder sbXML = new StringBuilder();
            ReceiptManager rcpMgr = null;

            try
            {
                sess = (Session)Application.Get(stSessID);
                if (null == sess)
                    return MessageManager.MSG_SES_001;

                rcpHdrDO = (ReceiptHeaderDO)XMLDocManager.Deserialize(typeof(ReceiptHeaderDO), XMLDocManager._XML_HEADER + stXML);

                if (rcpHdrDO == null)
                    return MessageManager.MSG_GEN_002; //反序列化对象失败

                rcpMgr = new ReceiptManager();
                return rcpMgr.SaveReceiptHdr(sess, rcpHdrDO);
            }
            catch (Exception e)
            {
                return MessageManager.MSG_GEN_000 + e.Message;
            }
        }

        [WebMethod]
        public string SaveReceiptDtl(string stSessID, string stRcpDtlXML)
        {
            ReceiptDetailDO rcpDtlDO = null;
            Session sess = null;
            StringBuilder sbXML = new StringBuilder();
            ReceiptManager rdMgr = null;

            try
            {
                rcpDtlDO = (ReceiptDetailDO)XMLDocManager.Deserialize(typeof(ReceiptDetailDO), XMLDocManager._XML_HEADER + stRcpDtlXML);

                sess = (Session)Application.Get(stSessID);
                if (null == sess)
                    return MessageManager.MSG_SES_001;

                rdMgr = new ReceiptManager();
                return rdMgr.SaveReceiptDtl(sess, rcpDtlDO);
            }
            catch (Exception e)
            {
                return MessageManager.MSG_GEN_000 + e.Message;
            }
        }

        [WebMethod]
        public string GetReceiptDtls(string stSessID, int iIntRecNum)
        {
            Session sess = null;
            DataTable dt = null;
            StringBuilder sbXML = null;

            try
            {
                if (iIntRecNum < 1)
                    return "MSG_RCP_003";

                sess = (Session)Application.Get(stSessID);
                if (null == sess)
                    return MessageManager.MSG_SES_001;

                dt = ReceiptDetailDO.GetTableByColumn(sess, "INTERNAL_RECEIPT_NUM", iIntRecNum);
                if (DataManager.IsEmpty(dt))
                    return null;
                sbXML = new StringBuilder();
                dt.TableName = "RECEIPT_DETAIL";
                dt.DataSet.DataSetName = "RECEIPT_DETAILS";
                dt.DataSet.WriteXml(new StringWriter(sbXML));

                return sbXML.ToString();
            }
            catch (Exception e)
            {
                return MessageManager.MSG_GEN_000 + e.Message;
            }
            finally
            {
            }
        }

        [WebMethod]
        public string CheckIn(string stSessID, string[] arRcpContXML, string stPrefXML)
        {
            ReceivingHelper helper = null;
            ReceiptContainerDO rcDO = null;
            ReceivingPreferenceDO prefDO = null;
            Session sess = null;
            StringBuilder sbErr = null;
            string stErr = null;

            try
            {
                sess = (Session)Application.Get(stSessID);
                if (null == sess)
                    return MessageManager.MSG_SES_001;

                helper = new ReceivingHelper();
                sbErr = new StringBuilder();
                foreach (string stRcpContXML in arRcpContXML)
                {
                    rcDO = (ReceiptContainerDO)XMLDocManager.Deserialize(typeof(ReceiptContainerDO), XMLDocManager._XML_HEADER + stRcpContXML);
                    prefDO = (ReceivingPreferenceDO)XMLDocManager.Deserialize(typeof(ReceivingPreferenceDO), XMLDocManager._XML_HEADER + stPrefXML);
                    //由于技术原因，Flex的客户端无法传递NULL值，因此NULL在XML中表现为‘’；这里主要表现在所有属性字段上
                    //因此，我们需要显示地把所有为‘’的属性置为NULL

                    if (StringManager.IsEmpty(rcDO.ATTRIBUTE1))
                        rcDO.ATTRIBUTE1 = null;
                    if (StringManager.IsEmpty(rcDO.ATTRIBUTE2))
                        rcDO.ATTRIBUTE2 = null;
                    if (StringManager.IsEmpty(rcDO.ATTRIBUTE3))
                        rcDO.ATTRIBUTE3 = null;
                    if (StringManager.IsEmpty(rcDO.ATTRIBUTE4))
                        rcDO.ATTRIBUTE4 = null;
                    if (StringManager.IsEmpty(rcDO.ATTRIBUTE5))
                        rcDO.ATTRIBUTE5 = null;
                    if (StringManager.IsEmpty(rcDO.ATTRIBUTE6))
                        rcDO.ATTRIBUTE6 = null;
                    if (StringManager.IsEmpty(rcDO.ATTRIBUTE7))
                        rcDO.ATTRIBUTE7 = null;
                    if (StringManager.IsEmpty(rcDO.ATTRIBUTE8))
                        rcDO.ATTRIBUTE8 = null;
                    stErr = helper.CheckIn(sess, rcDO, prefDO);
                    if (!StringManager.IsEmpty(stErr))
                        sbErr.AppendLine(stErr);
                }
                if (!StringManager.IsEmpty(sbErr))
                    return sbErr.ToString();
                else
                    return null;

            }
            catch (Exception e)
            {
                return MessageManager.MSG_GEN_000 + e.Message;
            }
            finally
            {
            }
        }

        [WebMethod]
        public string GetReceiptCont(string stSessID, string stWarehouse, string stIntRecNum)
        {
            DataTable dt = null;
            Session sess = null;
            StringBuilder sbXML = null;
            Dictionary<string, KeyValuePair<string, object>> dcCond = null;

            try
            {
                sess = (Session)Application.Get(stSessID);
                if (null == sess)
                    return MessageManager.MSG_SES_001;

                dcCond = new Dictionary<string, KeyValuePair<string, object>>();
                dcCond.Add("WAREHOUSE", new KeyValuePair<string, object>("=", stWarehouse));
                dcCond.Add("INTERNAL_RECEIPT_NUM", new KeyValuePair<string, object>("=", stIntRecNum));
                dt = ReceiptContainerDO.GetTableByColumns(sess, DictionaryManager.ToList(dcCond), null);
                if (DataManager.IsEmpty(dt))
                    return null;
                dt.TableName = "RECEIPT_CONTAINER";
                dt.DataSet.DataSetName = "RECEIPT_CONTAINERS";
                sbXML = new StringBuilder();
                dt.DataSet.WriteXml(new StringWriter(sbXML));

                return XMLDocManager.TransformStringWithFile(sbXML.ToString(), Server.MapPath(XSLT_DIR) + "\\ReceiptContainer.xslt", Encoding.UTF8);
            }
            catch (Exception e)
            {
                return MessageManager.MSG_GEN_000 + e.Message;
            }
        }

        [WebMethod]
        public string LocateReceiptContainers(string stSessID, int[] arRecContNum, string stPrefXML)
        {
            ReceivingHelper helper = null;
            Session sess = null;
            ReceivingPreferenceDO prefDO = null;

            try
            {
                sess = (Session)Application.Get(stSessID);
                if (null == sess)
                    return MessageManager.MSG_SES_001;

                prefDO = (ReceivingPreferenceDO)XMLDocManager.Deserialize(typeof(ReceivingPreferenceDO), XMLDocManager._XML_HEADER + stPrefXML);
                helper = new ReceivingHelper();
                return helper.LocateContainers(sess, arRecContNum, prefDO);
            }
            catch (Exception e)
            {
                return MessageManager.MSG_GEN_000 + e.Message;
            }
            finally
            {
                sess = null;
                helper = null;
            }
        }

        [WebMethod]
        public string UnlocateReceiptConts(string stSessID, int[] arRecContNum)
        {
            ReceivingHelper helper = null;
            Session sess = null;
            string stErr = null;
            StringBuilder sbErr = null;

            try
            {
                sess = (Session)Application.Get(stSessID);
                if (null == sess)
                    return MessageManager.MSG_SES_001;

                helper = new ReceivingHelper();
                sbErr = new StringBuilder();
                foreach (int iRecContNum in arRecContNum)
                {
                    stErr = helper.Unlocate(sess, iRecContNum);
                    if (!StringManager.IsEmpty(stErr))
                        sbErr.AppendLine(stErr);
                }
                if (!StringManager.IsEmpty(sbErr))
                    return sbErr.ToString();
                return null;
            }
            catch (Exception e)
            {
                return MessageManager.MSG_GEN_000 + e.Message;
            }
            finally
            {
                sess = null;
                helper = null;
            }
        }

        [WebMethod]
        public string SaveAppointment(string stSessID, string stXML)
        {
            AppointmentDO apptDO = null;
            Session sess = null;
            StringBuilder sbXML = new StringBuilder();

            try
            {
                sess = (Session)Application.Get(stSessID);
                if (null == sess)
                    return MessageManager.MSG_SES_001;

                apptDO = (AppointmentDO)XMLDocManager.Deserialize(typeof(AppointmentDO), XMLDocManager._XML_HEADER + stXML);
                if (apptDO.INTERNAL_APPOINT_NUM > 0)
                {
                    apptDO.Update(sess);
                    return "" + apptDO.INTERNAL_APPOINT_NUM;
                }
                else
                    return apptDO.Insert(sess);
            }
            catch (Exception e)
            {
                return MessageManager.MSG_GEN_000 + e.Message;
            }
        }

        [WebMethod]
        public string GetAppointments(string stSessID, string stCondXML)
        {
            List<KeyValuePair<string, KeyValuePair<string, object>>> dcCond = null;
            DataTable dt = null;
            Session sess = null;
            StringBuilder sbXML = null;
            //string stRsltXML = null;

            try
            {
                sess = (Session)Application.Get(stSessID);
                if (null == sess)
                    return MessageManager.MSG_SES_001;

                dcCond = new List<KeyValuePair<string, KeyValuePair<string, object>>>();
                if (!StringManager.IsEmpty(stCondXML))
                    dcCond = XMLDocManager.BuildQueryCondition(stCondXML);


                dt = AppointmentDO.GetTableByColumns(sess, dcCond, null);
                if (DataManager.IsEmpty(dt))
                    return null;
                sbXML = new StringBuilder();
                dt.TableName = "APPOINTMENT";
                dt.DataSet.DataSetName = "APPOINTMENTS";
                dt.DataSet.WriteXml(new StringWriter(sbXML));
                return sbXML.ToString();
            }
            catch (Exception e)
            {
                return MessageManager.MSG_GEN_000 + e.Message;
            }
            finally
            {
            }
        }

        [WebMethod]
        public string CancelCheckIn(string stSessID, int[] arRecContNum)
        {
            ReceivingHelper helper = null;
            //ReceiptManager mgr = null;
            Session sess = null;
            string stErr = null;
            StringBuilder sbErr = null;

            try
            {
                sess = (Session)Application.Get(stSessID);
                if (null == sess)
                    return MessageManager.MSG_SES_001;

                helper = new ReceivingHelper();
                foreach (int iRecContNum in arRecContNum)
                {
                    stErr = helper.CancelCheckIn(sess, iRecContNum);
                    if (!StringManager.IsEmpty(stErr))
                        sbErr.AppendLine(stErr);
                }

                if (!StringManager.IsEmpty(sbErr))
                    return sbErr.ToString();
                return null;
            }
            catch (Exception e)
            {
                return MessageManager.MSG_GEN_000 + e.Message;
            }
            finally
            {
            }
        }

        [WebMethod]
        public string DeleteReceipts(string stSessID, int[] arIntShpNum)
        {
            Session sess = null;
            ReceiptManager mgr = null;
            StringBuilder sbError = null;
            string stError = null;

            try
            {
                sess = (Session)Application.Get(stSessID);
                if (null == sess)
                    return MessageManager.MSG_SES_001;

                mgr = new ReceiptManager();
                sbError = new StringBuilder();
                for (int i = 0; i < arIntShpNum.Length; i++)
                {
                    stError = mgr.DeleteReceipt(sess, arIntShpNum[i]);
                    if (!StringManager.IsEmpty(stError))
                    {
                        sbError.AppendLine(stError);
                    }
                }

                if (StringManager.IsEmpty(sbError))
                    return null;
                else
                    return sbError.ToString();
            }
            catch (Exception e)
            {
                return MessageManager.MSG_GEN_000 + e.Message;
            }
        }

        [WebMethod]
        public string DeleteReceiptDetail(string stSessID, int iIntShpLnNum)
        {
            Session sess = null;
            ReceiptManager mgr = null;

            try
            {
                sess = (Session)Application.Get(stSessID);
                if (null == sess)
                    return MessageManager.MSG_SES_001;

                mgr = new ReceiptManager();
                return mgr.DeleteReceiptDtl(sess, iIntShpLnNum);
            }
            catch (Exception e)
            {
                return MessageManager.MSG_GEN_000 + e.Message;
            }
        }

        [WebMethod]
        public string GetDefaultPref(string stSessID, string stPrefName)
        {
            DataTable dt = null;
            Session sess = null;
            StringBuilder sbXML = null;
            UserDO usrDO = null;

            try
            {
                sess = (Session)Application.Get(stSessID);
                if (null == sess)
                    return MessageManager.MSG_SES_001;

                if (StringManager.IsEmpty(stPrefName))
                {
                    //获取用户默认的选项名称
                    usrDO = UserDO.GetModelByPK(sess, sess.User);

                    //如果没有指定的选项， 使用默认值
                    if (usrDO == null)                        
                        stPrefName = Constants.stSTARDEFAULT; 
                    else
                        stPrefName = usrDO.DEFAULT_RECV_PREF;
                }

                dt = ReceivingPreferenceDO.GetTableByColumn(sess, "PREFERENCE_NAME", stPrefName);
                if (DataManager.IsEmpty(dt))
                    return null;
                sbXML = new StringBuilder();
                dt.TableName = "RECEIVING_PREFERENCE";
                dt.DataSet.DataSetName = "RECEIVING_PREFERENCES";
                dt.DataSet.WriteXml(new StringWriter(sbXML));
                return sbXML.ToString();
            }
            catch (Exception e)
            {
                return MessageManager.MSG_GEN_000 + e.Message;
            }
            finally
            {
            }
        }

        [WebMethod]
        public string CreateReceiptFromPO(string stSessID, int iPONum, string stReceiptID, int[] arLineNum, int[] arLineQty)
        {
            Session sess = null;
            ReceiptManager mgr = null;

            try
            {
                sess = (Session)Application.Get(stSessID);
                if (null == sess)
                    return MessageManager.MSG_SES_001;
                mgr = new ReceiptManager();
                return mgr.GenReceiptFromPO(sess, iPONum, stReceiptID, arLineNum, arLineQty);
            }
            catch (Exception e)
            {
                ExceptionManager.LogException(sess, e);
                return MessageManager.MSG_GEN_000;
            }
            finally
            {
            }
        }

        [WebMethod]
        public string CloseReceipt(string stSessID, int iReceiptNum)
        {
            Session sess = null;
            ReceivingHelper mgr = null;

            try
            {
                sess = (Session)Application.Get(stSessID);
                if (null == sess)
                    return MessageManager.MSG_SES_001;
                mgr = new ReceivingHelper();
                return mgr.CloseReceipt(sess, iReceiptNum);
            }
            catch (Exception e)
            {
                ExceptionManager.LogException(sess, e);
                return MessageManager.MSG_GEN_000;
            }
            finally
            {
            }
        }

        [WebMethod]
        public string DeletePOs(string stSessID, int[] arIntPONum)
        {
            Session sess = null;
            POManager mgr = null;
            StringBuilder sbError = null;
            string stError = null;

            try
            {
                sess = (Session)Application.Get(stSessID);
                if (null == sess)
                    return MessageManager.MSG_SES_001;

                mgr = new POManager();
                sbError = new StringBuilder();
                for (int i = 0; i < arIntPONum.Length; i++)
                {
                    stError = mgr.DeletePO(sess, arIntPONum[i]);
                    if (!StringManager.IsEmpty(stError))
                    {
                        sbError.AppendLine(stError);
                    }
                }

                if (StringManager.IsEmpty(sbError))
                    return null;
                else
                    return sbError.ToString();
            }
            catch (Exception e)
            {
                return MessageManager.MSG_GEN_000 + e.Message;
            }
        }

        [WebMethod]
        public string DeletePODetail(string stSessID, int iIntPOLnNum)
        {
            Session sess = null;
            POManager mgr = null;

            try
            {
                sess = (Session)Application.Get(stSessID);
                if (null == sess)
                    return MessageManager.MSG_SES_001;

                mgr = new POManager();
                return mgr.DeletePODetail(sess, iIntPOLnNum);
            }
            catch (Exception e)
            {
                return MessageManager.MSG_GEN_000 + e.Message;
            }
        }

        [WebMethod]
        public string SaveReceiptCont(string stSessID, int iRecContNum, string stLocatingRule, string stToLoc, string stInvSts)
        {
            ReceiptContainerDO rcpCntDO = null;
            Session sess = null;
            StringBuilder sbXML = new StringBuilder();
            ReceiptContManager rdMgr = null;

            try
            {
                //rcpCntDO = (ReceiptContainerDO)XMLDocManager.Deserialize(typeof(ReceiptContainerDO), XMLDocManager._XML_HEADER + stRcpDtlXML);
                rcpCntDO = new ReceiptContainerDO();
                rcpCntDO.INTERNAL_CONTAINER_NUM = iRecContNum;
                rcpCntDO.LOCATING_RULE = stLocatingRule;
                rcpCntDO.TO_LOCATION = stToLoc;
                rcpCntDO.INVENTORY_STS = stInvSts;

                sess = (Session)Application.Get(stSessID);
                if (null == sess)
                    return MessageManager.MSG_SES_001;

                rdMgr = new ReceiptContManager();
                return rdMgr.SaveUpdateFromUI(sess, rcpCntDO);
            }
            catch (Exception e)
            {
                return MessageManager.MSG_GEN_000 + e.Message;
            }
        }
    }
}
